<template>
  <el-card>
    <el-form :inline="true">
      <el-form-item label="一级分类">
        <!--v-model 用来收集option中value绑定的值 -->
        <el-select v-model="categoryStore.c1Id" @change="selectC1" :disabled=disableSelect style="width: 150px" >
          <el-option v-for="(c1, index) in categoryStore.c1Arr" :label="c1.name" :value="c1.id"  :key="index" />
        </el-select>
      </el-form-item>

      <el-form-item label="二级分类">
        <el-select  v-model="categoryStore.c2Id" @change="selectC2" :disabled=disableSelect style="width: 150px">
          <el-option v-for="(c2, index) in categoryStore.c2Arr" :label="c2.name" :value="c2.id" :key="index" />
        </el-select>
      </el-form-item>

      <el-form-item label="三级分类">
        <el-select v-model="categoryStore.c3Id" @change="selectC3" :disabled=disableSelect style="width: 150px">
          <el-option v-for="(c3, index) in categoryStore.c3Arr" :label="c3.name" :value="c3.id" :key="index" />
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup lang="ts">

import { onMounted} from 'vue'
import { useCategoryStore } from '@/stores/modules/category'

defineProps(['disableSelect'])
// categoryStore
const categoryStore = useCategoryStore()

// 一级分类改变的监听
const selectC1 = () => {
  // 清空c2Arr和c3Arr
  categoryStore.c2Arr = []
  categoryStore.c3Arr = []
  categoryStore.c2Id = ''
  categoryStore.c3Id = ''

  // 请求二级分类
  categoryStore.reqCategory2List(categoryStore.c1Id)
}

// 二级分类改变的监听
const selectC2 = () => {
  // 清空c3Arr
  categoryStore.c3Arr = []
  categoryStore.c3Id = ''

  // 请求三级分类
  categoryStore.reqCategory1ListCategory3List(categoryStore.c2Id)
}

// 三级分类改变的监听
const selectC3 = () => {
  // 获取三级分类id
  // todo ...
}

// 页面加载
onMounted(() => {
  categoryStore.reqCategory1List();
})

</script>

<style lang="scss" scoped></style>
